import { _decorator, Button, Component, find, instantiate, Label, loader, log, Node, Prefab } from 'cc';
import UtilTool from './UtilTool';
const { ccclass, property } = _decorator;

@ccclass('showPrefab')
export class ShowTip extends Component {

    @property({ type: Number || String, tooltip: '提示框' })
    _alert
    @property({ type: Number || String, tooltip: '提示框确定按钮' })
    _btnOK
    @property({ type: Number || String, tooltip: '提示框取消按钮' })
    _btnCancel
    @property({ type: Number || String, tooltip: '提示框标题' })
    _title
    @property({ type: Number || String, tooltip: '提示框内容' })
    _content
    @property({ type: Number || String, tooltip: '点击确定按钮的回调事件' })
    _btnOKCallback
    @property({ type: Number || String, tooltip: '动画速度' })
    _animSpeed: 0.3


    onLoad() {

        //配置AppStart.js以后才可以判断

        // if(  vv == null){

        // return;

        // }

        log('Alert.js onLoad');

        this._alert = find("Canvas/safe_area/alert");

        this._title = find("Canvas/safe_area/alert/background/title").getComponent(Label);

        this._content = find("Canvas/safe_area/alert/background/content").getComponent(Label);

        this._btnOK = find("Canvas/safe_area/alert/background/btn_ok").getComponent(Button);

        this._btnCancel = find("Canvas/safe_area/alert/background/btn_cancel").getComponent(Button);

        if (this._btnCancel instanceof Button) {

            UtilTool.printLog('是个Button ');

        } else {

            UtilTool.printLog('是个鬼');

        }

        this._btnOK.active = false;

        //下面这段代码开启是否显示

        // this._alert.active = false;

        // vv.alert = this;

    }

    showTip(str) {
        //我們先動態取得Canvas
        var CanvasNode = find('Canvas');
        if (!CanvasNode) { log('find Canvas error'); return; }

        var onResourceLoaded = function (errorMessage, loadedResource) {

            if (errorMessage) { log('Prefab error:' + errorMessage); return; }
            if (!(loadedResource instanceof Prefab)) { log('Prefab error'); return; }

            var newMyPrefab = instantiate(loadedResource);

            CanvasNode.addChild(newMyPrefab);

            newMyPrefab.setPosition(0, 0);

            // var newMyPrefabScript = newMyPrefab.getComponent('showTips');


            // newMyPrefabScript.setLabelString(str);
        };
        // load('prefabs/tips', onResourceLoaded);
    }


    onBtnClicked(event) {
        if (event.target.name == "btn_ok") {

            if (this._btnOKCallback) {

                this._btnOKCallback();

            }

        }
        this._alert.active = false;

        this._btnOKCallback = null;

        UtilTool.printLog("这是全新定义的clicked!!");
    }

    cancelBtnClicked() {

        log('我被点中了');

        this._alert.active = false;

    }

    /**
    
    * title:弹框标题
    
    * content：弹框显示内容
    
    * callback：点击“确定”按钮的回调事件
    
    * needCancel：是否需要显示“取消”按钮
    
    */

    show(title, content, callback, needCancel) {

        UtilTool.printLog('paras -----> : ', title, content, callback, needCancel);

        this._alert.active = true;

        this._btnOKCallback = callback;

        this._title.string = title;

        this._content.string = content;

        if (needCancel) {

            // "确定" 和 "取消"都显示

            //注意：这里面都是对节点node 操作,this._btnCancel.active(或者.x)什么的操作都是无效的☹️

            UtilTool.printLog("needCancel ? true");

            this._btnCancel.node.active = true;

            this._btnOK.node.x = -239.5;

            this._btnCancel.node.x = 239.5;

            if (this._btnOK) {

                log('也是存在的啊！');

            }

        }

        else {

            //不需要显示“取消”按钮

            UtilTool.printLog("needCancel ? false");

            this._btnCancel.node.active = false;

            this._btnOK.node.x = 0;

        }

    }

    update(deltaTime: number) {

    }
}